package idiet.wizards.page;

import idiet.Activator;
import idiet.bean.Customer;
import idiet.service.CustomerService;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

import swing2swt.layout.BorderLayout;

public class WizardPageCustomerTraceQuery extends WizardPage {
	private Table table;
	private Text textCustomerName;
	private DateTime dateTimeStart;
	private DateTime dateTimeEnd;
	private TableViewer tableViewer;
	private List<Customer> customers=new ArrayList<Customer>();
	private CustomerService customerService=Activator.getApplicationContext().getBean(CustomerService.class);
	private Customer selection;
	/**
	 * Create the wizard.
	 */
	public WizardPageCustomerTraceQuery() {
		super("wizardPage");
		setTitle("\u7B2C\u4E00\u6B65\uFF1A\u67E5\u8BE2\u5BA2\u6237\u4FE1\u606F");
		setDescription("请选择客户信息录入起止时间或者输入客户姓名，然后点击搜索");
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		container.setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new GridLayout(5, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setText("Please enter date of customer's information enter");
		
		dateTimeStart = new DateTime(composite, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
		GridData gd_dateTimeStart = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_dateTimeStart.widthHint = 181;
		dateTimeStart.setLayoutData(gd_dateTimeStart);
		
		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setText("-");
		
		dateTimeEnd = new DateTime(composite, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
		GridData gd_dateTimeEnd = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_dateTimeEnd.widthHint = 161;
		
		new Label(composite, SWT.NONE);
		Calendar calendar=GregorianCalendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 30);
		dateTimeEnd.setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		dateTimeEnd.setLayoutData(gd_dateTimeEnd);
		
		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("Customer Name");
		
		textCustomerName = new Text(composite, SWT.BORDER);
		textCustomerName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1));
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_btnNewButton.widthHint = 85;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("Query");
		btnNewButton.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				SimpleDateFormat format=new SimpleDateFormat("M/d/yyyy");
				//customer.setForeStartDate(format.parse(StringUtils.substringBetween(dateTimeStart.toString(), "{", "}")));
				if(textCustomerName.getText().trim().equals("")){
					
					if(dateTimeStart.toString().equals(dateTimeEnd.toString())){
						customers=customerService.getCustomers(null);
					}else{
						try {
							customers=customerService.getCustomers(null, new Timestamp(format.parse(StringUtils.substringBetween(dateTimeStart.toString(), "{", "}")).getTime()), new Timestamp(format.parse(StringUtils.substringBetween(dateTimeEnd.toString(), "{", "}")).getTime()));
						} catch (ParseException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}else{
					if(dateTimeStart.toString().equals(dateTimeEnd.toString())){
						customers=customerService.getCustomers(textCustomerName.getText());
					}else{
						try {
							customers=customerService.getCustomers(textCustomerName.getText(), new Timestamp(format.parse(StringUtils.substringBetween(dateTimeStart.toString(), "{", "}")).getTime()), new Timestamp(format.parse(StringUtils.substringBetween(dateTimeEnd.toString(), "{", "}")).getTime()));
						} catch (ParseException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
				tableViewer.setInput(customers);
			}
		});
		
		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayout(new BorderLayout(0, 0));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		tableViewer = new TableViewer(composite_1, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(BorderLayout.CENTER);
		table.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				IStructuredSelection selections =(IStructuredSelection)tableViewer.getSelection();
				Customer customer=(Customer)selections.getFirstElement();
				selection=customer;
			}
			
		});
		tableViewer.setContentProvider(new ArrayContentProvider());
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("Name");
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getName();
			}
		});
		
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("Gender");
		tableViewerColumn_3.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getGender();
			}
		});
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("Nation");
		tableViewerColumn_2.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getNation().getName();
			}
		});
		
		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_4 = tableViewerColumn_4.getColumn();
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText("Phone");
		tableViewerColumn_4.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element){
				return ((Customer)element).getTelephone();
			}
		});
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("Cellphone");
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getCellphone();
			}
		});
		
		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_5 = tableViewerColumn_5.getColumn();
		tblclmnNewColumn_5.setWidth(100);
		tblclmnNewColumn_5.setText("Im");
		tableViewerColumn_5.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getIm();
			}
		});
		
		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_6 = tableViewerColumn_6.getColumn();
		tblclmnNewColumn_6.setWidth(100);
		tblclmnNewColumn_6.setText("Adress");
		tableViewerColumn_6.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getAddress();
			}
		});
	}

	@Override
	public IWizardPage getNextPage() {
		// TODO Auto-generated method stub
		WizardPageCustomerTraceUpdate next=(WizardPageCustomerTraceUpdate)super.getNextPage();
		next.setCustomer(selection);
		return super.getNextPage();
	}
	
}
